<template>
  <div class="register-container">
    <div class="register-box">
      <div class="register-header">
        <h2 class="register-title">教育平台 - 学生注册</h2>
      </div>
      <div class="register-form">
        <el-form ref="registerForm" :model="registerForm" :rules="registerRules" label-width="0px">
          <el-form-item prop="username">
            <el-input v-model="registerForm.username" prefix-icon="el-icon-user" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item prop="name">
            <el-input v-model="registerForm.name" prefix-icon="el-icon-user" placeholder="请输入姓名"></el-input>
          </el-form-item>
          <el-form-item prop="email">
            <el-input v-model="registerForm.email" prefix-icon="el-icon-message" placeholder="请输入邮箱"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="registerForm.password" prefix-icon="el-icon-lock" type="password" placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-form-item prop="confirmPassword">
            <el-input v-model="registerForm.confirmPassword" prefix-icon="el-icon-lock" type="password" placeholder="请确认密码"></el-input>
          </el-form-item>
          <el-form-item prop="schoolId">
            <el-select v-model="registerForm.schoolId" placeholder="请选择学校" style="width: 100%;">
              <el-option
                v-for="item in schools"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-checkbox v-model="registerForm.agreement">我已阅读并同意<a href="javascript:;" class="agreement-link">用户协议</a></el-checkbox>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :loading="loading" class="register-button" @click="handleRegister">注册</el-button>
          </el-form-item>
        </el-form>
        <div class="register-actions">
          <p>已有账号? <router-link to="/login" class="login-link">立即登录</router-link></p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Register',
  data() {
    // 密码确认验证
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.registerForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    
    // 协议验证
    const validateAgreement = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请阅读并同意用户协议'))
      } else {
        callback()
      }
    }
    
    return {
      registerForm: {
        username: '',
        name: '',
        email: '',
        password: '',
        confirmPassword: '',
        schoolId: '',
        agreement: false
      },
      registerRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { validator: validatePass, trigger: 'blur' }
        ],
        schoolId: [
          { required: true, message: '请选择学校', trigger: 'change' }
        ],
        agreement: [
          { validator: validateAgreement, trigger: 'change' }
        ]
      },
      schools: [
        { id: 1, name: '北京大学' },
        { id: 2, name: '清华大学' },
        { id: 3, name: '复旦大学' },
        { id: 4, name: '上海交通大学' },
        { id: 5, name: '浙江大学' }
      ],
      loading: false
    }
  },
  methods: {
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true
          // 模拟注册请求
          setTimeout(() => {
            this.$message({
              type: 'success',
              message: '注册成功!'
            })
            this.$router.push('/login')
            this.loading = false
          }, 1500)
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #e0f7fa 0%, #80deea 100%);
}

.register-box {
  width: 400px;
  padding: 30px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  margin: 40px 0;
}

.register-header {
  text-align: center;
  margin-bottom: 30px;
}

.register-title {
  font-size: 24px;
  color: #409EFF;
  margin: 0;
}

.register-button {
  width: 100%;
}

.register-actions {
  margin-top: 20px;
  text-align: center;
  color: #606266;
}

.agreement-link, .login-link {
  color: #409EFF;
  text-decoration: none;
  
  &:hover {
    text-decoration: underline;
  }
}

@media (max-width: 768px) {
  .register-box {
    width: 90%;
    max-width: 400px;
  }
}
</style> 